<?php
/**
 * Hide Referrer  
 * Don't allow external sites to see which wiki page is linking to them.
 * @license GPL
*/

if(!defined('MEDIAWIKI')) {
	echo "This file is an extension to the MediaWiki software and cannot be used standalone.";
	die(1);
}

$wgExtensionCredits['other'][] = array(
	'name'           => 'Hide Referring Page',
	'author'         => 'Tony Primerano, Francisco Santos',
	'url'            => '',
	'version'        => '1.1',
	'description'    => 'Don\'t allow external sites to see which wiki page is linking to them.',
	'descriptionmsg' => 'hidereferringpage-desc',
);

$wgExtensionMessagesFiles['Extimgwl'] = dirname(__FILE__) . '/HideReferringPage.i18n.php';

$wgHooks['LinkerMakeExternalLink'][] = 'modifyExternalLinks';
$wgHooks['LinkerMakeExternalImage'][] = 'modifyExternalImages';


function modifyExternalLinks(&$url, &$text, &$link, &$attribs, $linktype) {
	global $wgScriptPath, $wgServer, $wgHideReferringPageChain;
	
	$target='';
	if (isset($attribs['target'])) {
		$target=' target="'.$attribs['target'].'"';
	}
	
	$base=$wgServer.$wgScriptPath.'/extensions/HideReferringPage/redir.php?url=';
	if (isset($wgHideReferringPageChain)) {
		$destination=$base.urlencode($wgHideReferringPageChain.$url);
	} else {
		$destination=$base.urlencode($url);
	}
	
	$link = '<a href="'.$destination.'" class="external text" rel="nofollow" title="'.$url.'"'.$target.'>'.$text.'</a>';

	return false;
}

function modifyExternalImages(&$url, &$alt, &$img) {
	global $wgScriptPath, $wgServer, $wgHideReferringPageChain;
	
	$base=$wgServer.$wgScriptPath.'/extensions/HideReferringPage/redir.php?url=';
	$img = '<img src="'.$base.urlencode($url).'" alt="'.$alt.'" />';
	return false;

}
